package com.videoaggre.zcl.ui.dialog

import android.content.Context
import android.view.View
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.AgefansDetailsSerie
import com.videoaggre.zcl.common.bindViewClick
import com.videoaggre.zcl.common.toColor
import com.videoaggre.zcl.ui.fragment.agefans.AgefansDetailsSerieAdapter
import com.videoaggre.zcl.utils.Logger
import kotlinx.android.synthetic.main.popup_series.view.*

class SeriesPopup(context: Context) : BottomPopupView(context), View.OnClickListener {
    var onClickAdapterListenter: ((agefansDetailsSerie: AgefansDetailsSerie) -> Unit)? = null
    private val mAgefansDetailsSerieAdapter: AgefansDetailsSerieAdapter = AgefansDetailsSerieAdapter()
    override fun getImplLayoutId() = R.layout.popup_series
    private var mPositiveSequenceSeries: MutableList<AgefansDetailsSerie> = mutableListOf()
    private var mReverseOrderSeries: MutableList<AgefansDetailsSerie> = mutableListOf()
    override fun onCreate() {
        super.onCreate()
        bindViewClick(this, tvPositiveSequence, tvReverseOrder)
        recyclerView.layoutParams.apply { height = maxHeight }
        recyclerView.layoutManager = FlexboxLayoutManager(context).apply { flexDirection = FlexDirection.ROW; flexWrap = FlexWrap.WRAP;alignItems = AlignItems.STRETCH }
        recyclerView.adapter = mAgefansDetailsSerieAdapter
        mAgefansDetailsSerieAdapter.setOnClickAdapterListenter { view: View, position: Int -> onClickAdapterListenter?.invoke(mAgefansDetailsSerieAdapter.getItem(position)) }
    }

    fun changeSerie(series: MutableList<AgefansDetailsSerie>) {
        mPositiveSequenceSeries = ArrayList(series)
        mReverseOrderSeries = ArrayList(series).also { it.reverse() }
        mAgefansDetailsSerieAdapter.datas = mReverseOrderSeries
        mAgefansDetailsSerieAdapter.notifyDataSetChanged()
    }

    override fun onShow() {
        tvAll.text = "全部(${mPositiveSequenceSeries.size})"
        super.onShow()
    }

    override fun onClick(v: View?) {
        when (v) {
            tvPositiveSequence -> {
                tvPositiveSequence.setTextColor(R.color.main_color.toColor())
                tvReverseOrder.setTextColor(R.color.text_color.toColor())
                mAgefansDetailsSerieAdapter.datas = mPositiveSequenceSeries
            }
            tvReverseOrder -> {
                tvReverseOrder.setTextColor(R.color.main_color.toColor())
                tvPositiveSequence.setTextColor(R.color.text_color.toColor())
                mAgefansDetailsSerieAdapter.datas = mReverseOrderSeries
            }
        }
        mAgefansDetailsSerieAdapter.notifyDataSetChanged()
    }

    override fun getMaxHeight() = (XPopupUtils.getScreenHeight(context) * .8f).toInt()
}